package com.smec.moblie.util;

import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Html.ImageGetter;
import android.util.Log;
import android.view.Display;
import android.widget.TextView;

public class URLImageGetter implements ImageGetter {
	TextView textView;
	Context context;

	public URLImageGetter(Context contxt, TextView textView) {
		this.context = contxt;
		this.textView = textView;
	}

	@Override
	public Drawable getDrawable(String paramString) {
		URLDrawable urlDrawable = new URLDrawable(context);

		ImageGetterAsyncTask getterTask = new ImageGetterAsyncTask(urlDrawable);
		getterTask.execute(paramString);
		return urlDrawable;
	}

	public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
		URLDrawable urlDrawable;

		public ImageGetterAsyncTask(URLDrawable drawable) {
			this.urlDrawable = drawable;
		}

		@Override
		protected void onPostExecute(Drawable result) {
			if (result != null) {
				urlDrawable.drawable = result;

				URLImageGetter.this.textView.requestLayout();
			}
		}

		@Override
		protected Drawable doInBackground(String... params) {
			String source = params[0];
			return fetchDrawable(source);
		}

		public Drawable fetchDrawable(String url) {
			Drawable drawable = null;
			URL Url;
			try {
				Log.i("xxx", "~~ fetch image  url :"+url );
				Url = new URL(url);
				drawable = Drawable.createFromStream(Url.openStream(), "");
			} catch (Exception e) {
				Log.w("xxx", "~~ fetch image from url error:"+url +" msg:"+e.toString());
				return null;
			}

			if(drawable==null){
				return  null;
			}
			// 按比例缩放图片
			Rect bounds = getDefaultImageBounds(context);
			int newwidth = bounds.width();
			int newheight = bounds.height();
//			double factor = 1;
//			double fx = (double) drawable.getIntrinsicWidth() / (double) newwidth;
//			double fy = (double) drawable.getIntrinsicHeight() / (double) newheight;
//			factor = fx > fy ? fx : fy;
//			if (factor < 1)
//				factor = 1;
//			newwidth = (int) (drawable.getIntrinsicWidth() / factor);
//			newheight = (int) (drawable.getIntrinsicHeight() / factor);

			//图标大小x5的大小，  如果x5大于了屏幕长宽，则用原图大小
			if(  (double) drawable.getIntrinsicWidth() < (newwidth/5) ){
				newwidth =(int) ((double) drawable.getIntrinsicWidth())*5;
				newheight =(int) ((double) drawable.getIntrinsicHeight())*5;
			}else if(newwidth < (double) drawable.getIntrinsicWidth() ){
				//如果图片大小大于了屏幕长度，则用屏幕宽度
			}else{
				//否则使用图片原宽度
				newwidth =(int) ((double) drawable.getIntrinsicWidth());
				newheight =(int) ((double) drawable.getIntrinsicHeight());
			}


			Log.i("xxx", "~~ newwidth :"+newwidth );
			Log.i("xxx", "~~ newheight :"+newheight );
			drawable.setBounds(0, 0, newwidth, newheight);

//			int containerWidth = textView.getMeasuredWidth();
//			int height = (int) (containerWidth * drawable.getIntrinsicHeight() * 1.0 / drawable.getIntrinsicWidth());
//			height += height - drawable.getIntrinsicHeight();
//
//			if (containerWidth != 0)
//				drawable.setBounds(0, 0, containerWidth, height);
//			else
//				drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());


			return drawable;
		}
	}

	// 预定图片宽高比例为 4:3
	@SuppressWarnings("deprecation")
	public Rect getDefaultImageBounds(Context context) {
		Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
		int width = display.getWidth();
		int height = (int) (width * 9 / 16 );
		Rect bounds = new Rect(0, 0, width, height);
		return bounds;
	}
}